// stores/dept.js
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { deptAPI } from '@/api/dept'

export const useDeptStore = defineStore('dept', () => {
    // 状态
    const deptList = ref([])
    const currentDept = ref(null)
    const loading = ref(false)
    const dialogVisible = ref(false)
    const dialogType = ref('add') // 'add' 或 'edit'
    const formData = ref({
        dept_name: '',
        dept_code: '',
        parent_id: null,
        sort: 0,
        dept_desc: ''
    })
    const formRules = ref({
        dept_name: [{ required: true, message: '请输入部门名称' }],
        dept_code: [{ required: true, message: '请输入部门编码' }],
        sort: [{ required: true, message: '请输入排序号' }]
    })
    const formRef = ref(null)

    // 计算属性 - 树形结构部门列表
    const treeDeptList = computed(() => {
        const buildTree = (parentId = null) => {
            return deptList.value
                .filter(dept => dept.parent_id === parentId)
                .map(dept => ({
                    ...dept,
                    children: buildTree(dept.id)
                }))
        }
        return buildTree()
    })

    // 操作
    const fetchDepts = async () => {
        loading.value = true
        try {
            const { data } = await deptAPI.getDeptList()
            console.log('获取部门列表成功:', data)
            deptList.value = data
            console.log('获取部门列表成功 deptList:', deptList.value)
        } catch (error) {
            console.error('获取部门列表失败:', error)
            throw error
        } finally {
            loading.value = false
        }
    }

    const getDept = async (id) => {
        try {
            const { data } = await deptAPI.getDept(id)
            return data
        } catch (error) {
            console.error('获取部门详情失败:', error)
            throw error
        }
    }

    const addDept = async (deptData) => {
        try {
            const { data } = await deptAPI.addDept(deptData)
            deptList.value.push(data)
            dialogVisible.value = false
            return data
        } catch (error) {
            console.error('添加部门失败:', error)
            throw error
        }
    }

    const updateDept = async (deptData) => {
        try {
            const { data } = await deptAPI.updateDept(deptData)
            const index = deptList.value.findIndex(item => item.id === data.id)
            if (index !== -1) {
                deptList.value.splice(index, 1, data)
            }
            dialogVisible.value = false
            return data
        } catch (error) {
            console.error('更新部门失败:', error)
            throw error
        }
    }

    const deleteDept = async (id) => {
        try {
            await deptAPI.deleteDept(id)
            const index = deptList.value.findIndex(item => item.id === id)
            if (index !== -1) {
                deptList.value.splice(index, 1)
            }
        } catch (error) {
            console.error('删除部门失败:', error)
            throw error
        }
    }

    const showDialog = (type) => {
        dialogType.value = type
        dialogVisible.value = true
    }
    const setCurrentDept = (dept= null) => {
        currentDept.value = dept
    }

    const closeDialog = () => {
        dialogVisible.value = false
        currentDept.value = null
    }

    return {
        // 状态
        deptList,
        currentDept,
        loading,
        dialogVisible,
        dialogType,
        formData,
        formRules,
        formRef,
        // 计算属性
        treeDeptList,

        // 操作
        setCurrentDept,
        fetchDepts,
        getDept,
        addDept,
        updateDept,
        deleteDept,
        showDialog,
        closeDialog
    }
})